﻿using Authentication.Model.Interface;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Identifier;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.ServiceCommand
{
    public class RetrieveUserRolesServiceCommand : ProviderServiceCommand<IUserRoleProvider, RetrieveUserRolesRequest, RetrieveUserRolesResponse>
    {
        public RetrieveUserRolesServiceCommand(IUserRoleProvider providers) : base(providers) { }

        protected override void Validate(RetrieveUserRolesRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(RetrieveUserRolesResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int pageSize = 0, pageIndex = 0, totalRecord = 0;
            List<UserRoleRecord> records = null;
            var applicationKey = Request.Header.ApplicationKey;
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            var objects = Providers.RetrieveUserRules(applicationKey.Value, Request.UserName);
            if (objects != null)
            {
                records = objects.Select(x => new UserRoleRecord
                {
                    ApplicationKey = x.GuidKey,
                    ObjectIdentifier = x.ObjectId,
                    RoleIdentifier = new RoleIdentifier(x.RuleId).DisplayableIdentifier,
                    UserName = x.UserName,
                    Active = x.IsActive,
                    Object = new ObjectRecord
                    {
                        ApplicationKey = x.GuidKey,
                        ObjectIcon = x.Object.ObjectIconUrl,
                        ObjectIdentifier = x.ObjectId,
                        ObjectName = x.Object.ObjectNameEnglish,
                        ObjectUrl = x.Object.ObjectUrl,
                        ParentIdentifier = x.Object.ParentId
                    }
                }).ToList();
            }

            totalRecord = records == null ? 0 : records.Count;
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }
    }
}
